package com.hfxt.dao.impl;

import com.hfxt.dao.UsesDao;
import com.hfxt.entity.Uses;
import com.hfxt.tool.MyBatisUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

public class UsesDaoImpl implements UsesDao {

    //使用mybatis三个核心接口
    //SqlSessionFactoryBuilder 构建器
    //SqlSessionFactory  sqlSession工厂
    //SqlSession
    //操作数据库的增删改查的对象  SqlSession

    public int getCount() {
        //创建构建器
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        SqlSession session=null;
        try{
            //获得流对象 解析mybatis核心配置文件  得到连接参数
            InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
            //创建SqlSessionFactory工厂
            SqlSessionFactory factory=builder.build(is);
            //得到操作数据库的对象
            session=factory.openSession();
            //具体执行 dao接口包名+接口名+id名
            //int result=session.selectOne("com.hfxt.dao.UsesDao.xxx");
            int result=session.getMapper(UsesDao.class).getCount();
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }finally {
            session.close();
        }
    }

    @Override
    public List<Uses> getUses() {
        //创建构建器
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        SqlSession session=null;
        try{
            //获得流对象 解析mybatis核心配置文件  得到连接参数
            InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
            //创建SqlSessionFactory工厂
            SqlSessionFactory factory=builder.build(is);
            //得到操作数据库的对象
            session=factory.openSession();
            //具体执行 dao接口包名+接口名+id名
            //int result=session.selectOne("com.hfxt.dao.UsesDao.xxx");
            List<Uses> list=session.getMapper(UsesDao.class).getUses();
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            session.close();
        }
    }

    @Override
    public List<Uses> getUsesByCondition1(String name) {
        List<Uses> list=null;
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getSession();
            list=sqlSession.getMapper(UsesDao.class).getUsesByCondition1(name);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return list;
    }

    @Override
    public List<Uses> getUsesByCondition2(String name, Integer money) {
        List<Uses> list=null;
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getSession();
            list=sqlSession.getMapper(UsesDao.class).getUsesByCondition2(name,money);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return list;
    }

    @Override
    public List<Uses> getUsesByCondition3(Map<String, Object> maps) {
        List<Uses> list=null;
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getSession();
            list=sqlSession.getMapper(UsesDao.class).getUsesByCondition3(maps);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return list;
    }

    @Override
    public List<Uses> getUsesByCondition4(Uses uses) {
        List<Uses> list=null;
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getSession();
            list=sqlSession.getMapper(UsesDao.class).getUsesByCondition4(uses);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return list;
    }

    @Override
    public List<Uses> getUsesByCondition5(Uses uses) {
        List<Uses> list=null;
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getSession();
            list=sqlSession.getMapper(UsesDao.class).getUsesByCondition5(uses);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return list;
    }

    @Override
    public List<Uses> getUsesByCondition6(Integer[] roleids) {
        List<Uses> list=null;
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getSession();
            list=sqlSession.getMapper(UsesDao.class).getUsesByCondition6(roleids);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return list;
    }

    @Override
    public List<Uses> getUsesByCondition7(List<Integer> roleids) {
        List<Uses> list=null;
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getSession();
            list=sqlSession.getMapper(UsesDao.class).getUsesByCondition7(roleids);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return list;
    }

    @Override
    public List<Uses> getUsesByCondition8(String choice) {
        List<Uses> list=null;
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getSession();
            list=sqlSession.getMapper(UsesDao.class).getUsesByCondition8(choice);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return list;
    }

    @Override
    public int addUser(Uses uses) {
        int result=0;
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getSession();
            result=sqlSession.getMapper(UsesDao.class).addUser(uses);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return result;
    }


    public int addUser2(Uses uses) {
        int result=0;
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getSession();
            result=sqlSession.getMapper(UsesDao.class).addUser2(uses);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return result;
    }

    @Override
    public int addUser3(Uses uses) {
        int result=0;
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getSession();
            result=sqlSession.getMapper(UsesDao.class).addUser3(uses);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return result;
    }

    public int upateUser(Uses uses) {
        int result=0;
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getSession();
            result=sqlSession.getMapper(UsesDao.class).upateUser(uses);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return result;
    }

    @Override
    public int upateUser2(Uses uses) {
        int result=0;
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getSession();
            result=sqlSession.getMapper(UsesDao.class).upateUser2(uses);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return result;
    }

    @Override
    public int delUser(Integer userid) {
        int result=0;
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getSession();
            result=sqlSession.getMapper(UsesDao.class).delUser(userid);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return result;
    }

    @Override
    public List<Uses> queryList(Uses uses) {
        List<Uses> list=null;
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getSession();
            list=sqlSession.getMapper(UsesDao.class).queryList(uses);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return list;
    }

    @Override
    public int getCountByPage(Uses uses) {
        int result=0;
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getSession();
            result=sqlSession.getMapper(UsesDao.class).getCountByPage(uses);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return result;
    }

    @Override
    public List<Uses> getUsesByPage(Uses uses, Integer pageIndex, Integer pageSize) {
        List<Uses> list=null;
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getSession();
            list=sqlSession.getMapper(UsesDao.class).getUsesByPage(uses,pageIndex,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return list;
    }

    @Override
    //mybatis缓存
    //一级缓存  sqlSession级别的缓存
    //二级缓存sqlSessionFactory   跟sqlSession关闭没有关系
    public Uses getUserByUserId(Integer userid) {
        SqlSession sqlSession=MyBatisUtil.getSession();
        Uses user1=sqlSession.getMapper(UsesDao.class).getUserByUserId(userid);
        System.out.println(user1.getUsername());

        MyBatisUtil.closeSession();

        sqlSession=MyBatisUtil.getSession();
        Uses user2=sqlSession.getMapper(UsesDao.class).getUserByUserId(userid);
       System.out.println(user2.getUsername());
        //System.out.println(user1==user2);

        return user1;
    }
}
